<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Default controller.
 */
class Import_Controller extends Controller 
{
	public function index()
	{
		$db = new Database();
		
		$r1 = $db->query("SELECT * FROM existing_titles_textbooks where title_cn<>''");
		
		echo '<table border="1"><tr><th>Title</th><th>Photo</th><th>Exist?</th></tr>';
		
		foreach($r1 as $r)
		{
			echo '<tr>';
			echo '<td>'.$r->title_cn.'</td>';
			echo '<td>&#160;'.$r->photo1.'</td>';
			echo '<td>';
			if(file_exists('D:/nanhai/photo/all/'.$r->photo1) and $r->photo1!='')
			{
				echo 'yes';
			}
			echo '&#160;</td>';
			echo '</tr>';
		}

		echo '</table>';
	}
	
	public function textbooks()
	{
		$str1 = '';
		$str2 = '';
		$db = new Database();
		
		$r1 = $db->query('SELECT * FROM existing_titles_textbooks');
		
		foreach($r1 as $r)
		{
			if( empty($r->titile_cn) and empty($r->title_en) ) continue;
			
			$s = new Product_Model();
			
			$has = $s->where('title_cn', $r->title_cn)->where('title_en', $r->title_en)->find();
			
			if($has) $s = new Product_Model($s->id);
			
			$s->series = $r->series;
			$s->title_cn = str_replace("\n", '', $r->title_cn);
			$s->title_en = str_replace("\n", '', $r->title_en);
			$s->description_cn = $r->desc_cn;
			$s->description_en = $r->desc_en;
			$s->author_cn = $r->author_cn;
			$s->author_en = $r->author_en;
			$s->publisher_cn = $r->publisher_cn;
			$s->publisher_en = $r->publisher_en;
			$s->quantity = $r->Quantity;
			$s->weight = $r->Weight;
			$s->sku = $r->SKU;
			//$s->amount = $r->amount;
			$s->market_price = $r->Price;
			$s->price = $r->Price;
			$s->extra_handling = $r->extra;
			$s->photo_ext = ( ! empty($r->photo1) and file_exists('D:/nanhai/photo/all/'.$r->photo1) ) ? strtolower(file::extension($r->photo1)) : '';
						
			if($s->save())
			{			
				if( ! empty($r->category) )
				{
					$cat = explode('/', $r->category);
					
					foreach($cat as $c)
					{
						$c = str_replace(array('LL-', 'HC-', 'LLi'), array('', '', ''), $c);
						$c = trim($c);
	
						if($su = $db->query("select id from product_categories where title_en='".$c."' or title_cn='".$c."'"))
						{
							$n = 1;
							foreach($su as $item)
							{
								if($n == 1) 
								{
									$s->category_id = $item->id;
									$s->save();
									$n++;
								}
								
								$db->query('REPLACE into products_categories set category_id='.$item->id.', product_id='.$s->id);
							}
						}
					}
				}
				
				if( (! empty($r->photo1)) and file_exists('D:/nanhai/photo/all/'.$r->photo1) )
				{
					copy('D:/nanhai/photo/all/'.$r->photo1, 'D:/Aptana Studio/phproot/nanhai.com/resource/product/'.$s->id.'.'.$s->photo_ext);
					//$img = new Image('D:/nanhai/photo/all/'.$r->photo1);
					//$img->save('D:/Aptana Studio/phproot/nanhai.com/resource/product/'.$s->id.'.'.$s->photo_ext);
				}
			}
		}		
	}

	public function feature()
	{
		$db = new Database();
		$r1 = $db->query('SELECT * FROM featured_products');
		
		foreach($r1 as $r)
		{
			if( empty($r->titile_cn) and empty($r->title_en) ) continue;
			
			$pm = new Product_Model();
			
			$p = $pm->where('title_en', $r->title_en)->orwhere('title_cn', $r->title_cn)->find();
			
			if( ! empty($r->category) and $p->id)
			{
				$c = trim($r->category);
				
				echo $c.'<br/>';

				if($su = $db->query("select id from product_categories where title_en='".$c."' or title_cn='".$c."'"))
				{
					foreach($su as $item)
					{
						$db->query('replace into products_categories set featured=1, product_id='.$p->id.', category_id='.$item->id);
					}
				}
			}
		}
	}
	
	public function deltextbooks()
	{
		$db = new Database();
		$resource = realpath("resource/product").'/';
		
		$r1 = $db->query("select * from products where id in(select product_id from products_categories where  category_id in (select id from product_categories where left_id>=2 and right_id<=29));");
		
		$i=1;
		foreach($r1 as $r)
		{
			$source_file = $resource.$r->id.'.'.$r->photo_ext;
			$destination_file = 'D:/nanhai/textbooks/'.str_replace(array('/',"'",'"',' '), array('','','',''), $r->sku).'.'.$r->photo_ext;
			if($r->photo_ext!='' and file_exists($source_file))
			{
				echo $i.'. '.$destination_file."</br>";
				$i++;
				//myfuns::move($source_file, $destination_file);
				copy($source_file, 'D:/upload/'.$r->id.'.'.$r->photo_ext);
			}
		}
		
		//$db->query("delete from products where id in(select product_id from products_categories where  category_id in (select id from product_categories where left_id>=2 and right_id<=29));");
		//$db->query("delete from products_categories where category_id in (select id from product_categories where left_id>=2 and right_id<=29));");
	}
	
	public function path()
	{
		$path = realpath('resource/forum');
		
		echo $path;
		
		echo $path["dirname"] . "<br/>";
		echo $path["basename"] . "<br/>";
		echo $path["extension"] . "<br/>";

	}
}